﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace intTrans
{
    public partial class intTrans : Form
    {
        public intTrans()
        {
            InitializeComponent();
        }

        private void btn_browse_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog open_image = new OpenFileDialog();
                open_image.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.png; *.bmp; *.tif)|*.jpg; *.jpeg; *.gif; *.png; *.bmp; *.tif";
                open_image.Multiselect = true;
                if (open_image.ShowDialog() == DialogResult.OK)
                {
                    pb_original.Image = Image.FromFile(open_image.FileName);
                    foreach (string fn in open_image.FileNames)
                        lb_imnames.Items.Add(fn);

                    lb_imnames.SelectedIndex = lb_imnames.Items.IndexOf(open_image.FileName);
                    pb_original.BorderStyle = BorderStyle.None;
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Failed loading image", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog save_image = new SaveFileDialog();
                save_image.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.png; *.bmp; *.tif)|*.jpg; *.jpeg; *.gif; *.png; *.bmp; *.tif";
                if (pb_transformed.Image != null && save_image.ShowDialog() == DialogResult.OK)
                {
                    pb_transformed.Image.Save(save_image.FileName);
                }
                if (pb_transformed.Image == null)
                    MessageBox.Show("There is no picture to save!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception)
            {
                MessageBox.Show("Failed saving image", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_start_Click(object sender, EventArgs e)
        {
            btn_start.Enabled = false;
            if (pb_original.Image != null)
            {
                try
                {
                    switch (lb_trans.SelectedItem.ToString())
                    {
                        case "Median filter": MedianFilter Median = new MedianFilter(lb_imnames.SelectedItem.ToString(), cmb_kernel.SelectedItem.ToString());
                            pb_transformed.Image = Median.Transform(progressBar);
                            break;
                        case "Temporal median filter": string[] filenames = new string[lb_imnames.Items.Count];
                            for (int i = 0; i < lb_imnames.Items.Count; i++)
                                filenames[i] = lb_imnames.Items[i].ToString();
                            TempMedianFilter Temp = new TempMedianFilter(filenames);
                            pb_transformed.Image = Temp.Transform(progressBar);
                            break;
                        case "Mode filter": ModeFilter Mode = new ModeFilter(lb_imnames.SelectedItem.ToString(), cmb_kernel.SelectedItem.ToString());
                            pb_transformed.Image = Mode.Transform(progressBar);
                            break;
                        case "Harris detector": HarrisDetector MHD = new HarrisDetector(lb_imnames.SelectedItem.ToString());
                            pb_transformed.Image = MHD.Transform(progressBar);
                            break;
                        default: Console.WriteLine("valami más");
                            break;
                    }
                    pb_transformed.BorderStyle = BorderStyle.None;
                }
                catch (Exception ex)
                {
                    if (ex.GetType().ToString() == "System.NullReferenceException")
                        MessageBox.Show("Please select a transformation!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (ex.Message == "size error")
                        MessageBox.Show("Nem azonos a képek felbontása!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    if (ex.Message == "PixelFormat")
                        MessageBox.Show("Csak 32 és 24 bites képekre alkalmazható ez a szűrő!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else MessageBox.Show("Please browse for a picture.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            btn_start.Enabled = true;
        }

        private void lb_imnames_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lb_imnames.SelectedIndex >= 0)
            {
                pb_original.Image = Image.FromFile(lb_imnames.SelectedItem.ToString());
                pb_original.BorderStyle = BorderStyle.None;
            }
            else
            {
                pb_original.Image = null;
                pb_original.BorderStyle = BorderStyle.FixedSingle;
            }
        }

        private void btn_remove_Click(object sender, EventArgs e)
        {
            int index = lb_imnames.SelectedIndex;
            lb_imnames.Items.Remove(lb_imnames.SelectedItem);
            if (lb_imnames.Items.Count > index) lb_imnames.SelectedIndex = index;
            else if (lb_imnames.Items.Count > 0) lb_imnames.SelectedIndex = index - 1;
        }

        private void btn_remove_all_Click(object sender, EventArgs e)
        {
            lb_imnames.Items.Clear();
        }

        private void lb_trans_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lb_trans.SelectedItem != null)
            {
                if (lb_trans.SelectedItem.ToString() == "Mode filter")
                {
                    cmb_kernel.Items.Clear();
                    cmb_kernel.Items.Add("5x5");
                    cmb_kernel.Items.Add("7x7");
                    cmb_kernel.Items.Add("9x9");
                    cmb_kernel.Items.Add("13x13");
                    cmb_kernel.Enabled = true;
                    if (cmb_kernel.SelectedIndex == -1)
                    {
                        cmb_kernel.SelectedIndex = 0;
                    }
                }
                else if (lb_trans.SelectedItem.ToString() == "Median filter")
                {
                    cmb_kernel.Items.Clear();
                    cmb_kernel.Items.Add("3x3");
                    cmb_kernel.Items.Add("5x5");
                    cmb_kernel.Items.Add("7x7");
                    cmb_kernel.Enabled = true;
                    if (cmb_kernel.SelectedIndex == -1)
                    {
                        cmb_kernel.SelectedIndex = 0;
                    }
                }
                else
                {
                    cmb_kernel.Enabled = false;
                }
            }
        }
    }
}
